package chapter21;

import java.lang.reflect.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/2/25 14:27
 */
enum Color {
    RED, GREEN, BLUE;
}

class Student {
    private static final int MAX_NAME_LEN = 255;
}

public class FieldTestDemo {
    public static void main(String[] args) throws IllegalAccessException, NoSuchFieldException {
        List<String> obj = Arrays.asList("老马", "hello");
        Class<?> cls = obj.getClass();
        for (Field field : cls.getDeclaredFields()) {
            field.setAccessible(true);
            System.out.println(field.getName() + "-" + field.get(obj));
        }

        Field field = Student.class.getDeclaredField("MAX_NAME_LEN");
        int mod = field.getModifiers();
        System.out.println(Modifier.toString(mod));
        System.out.println("is public?" + Modifier.isPublic(mod));
        System.out.println("is staic?" + Modifier.isStatic(mod));
        System.out.println("is final?" + Modifier.isFinal(mod));
        System.out.println("is volatile?" + Modifier.isVolatile(mod));


        System.out.println("-----------------");

        Class<?> clazz = Integer.class;
        try {
            Method method = clazz.getDeclaredMethod("parseInt", new Class[]{String.class});
            System.out.println(method.invoke(null, "123"));
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        System.out.println("------------------constructor");
        try {
            Constructor<StringBuilder> constructor = StringBuilder.class.getConstructor(new Class[]{int.class});
            StringBuilder builder = constructor.newInstance(100);
            System.out.println(builder.toString());
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }

        System.out.println("---------------------");
        System.out.println(Object.class.isAssignableFrom(String.class));
        System.out.println(String.class.isAssignableFrom(String.class));
        System.out.println(List.class.isAssignableFrom(ArrayList.class));

        System.out.println("----------------------");
        String name = "[Ljava.lang.String;";
        try {
            Class<?> clss = Class.forName(name);
            System.out.println(String[].class.equals(clss));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        System.out.println("----------------------");
        String[] arr = new String[0];
        System.out.println(arr.getClass().getComponentType());

        System.out.println("-----------------------");
        int[] intArr = (int[]) Array.newInstance(int.class, 10);
        String[] stringArr = (String[]) Array.newInstance(String.class, 10);

        System.out.println(intArr);
        System.out.println(stringArr);

        System.out.println("----------------------");
        Color[] colors = Color.class.getEnumConstants();
        Arrays.stream(colors).forEach(System.out::println);

    }

    public static Class<?> forName(String className) throws ClassNotFoundException {
        if ("int".equals(className)) {
            return int.class;
        }
        return Class.forName(className);
    }

    public <T> T cast(Object object, Class<T> cls) {
        return cls.cast(object);
    }
}
